package com.batsoft.trade.common.task;

import org.apache.commons.lang3.concurrent.BasicThreadFactory;

import java.util.concurrent.*;

/**
 * @author <a href="mailto:HelloHeSir@gmail.com">HeC</a>
 * @date 2018/12/25 15:49
 */
public class ThreadPool {

    private static final String THREAD_NAME = "common-thread-%d";

    private static final Integer POOL_MIN = 5;

    private static final Integer POOL_SIZE = 50;

    public static final Long KEEP_ALIVE_TIME = 60000L;

    private static ThreadPoolExecutor threadPool;

    static {
        threadPool = new ThreadPoolExecutor(POOL_MIN, POOL_SIZE,
                KEEP_ALIVE_TIME, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>(), new BasicThreadFactory.Builder().namingPattern(THREAD_NAME).build());
    }

    /**
     * 运行线程
     *
     * @param runnable 传入一个new好的线程
     */
    public static void exe(Runnable runnable) {
        threadPool.execute(runnable);
    }

    /**
     * 运行线程
     *
     * @param callable 传入一个new好的线程
     */
    public static Future<?> submit(Callable callable) {
        return threadPool.submit(callable);
    }

}